package com.helloworld.chat.action;

import com.google.gson.Gson;
import com.helloworld.chat.dao.FriendRequestDao;
import com.helloworld.chat.entity.User;
import com.helloworld.chat.service.FriendRequestService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xdzy on 2017/6/30.
 */
@Controller
@Scope("prototype")
public class FriendRequestAction {
    private int id;
    private int receiverGid;
    private String result;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    @Autowired
    private FriendRequestService friendRequestService;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getReceiverGid() {
        return receiverGid;
    }

    public void setReceiverGid(int receiverGid) {
        this.receiverGid = receiverGid;
    }

    public void setFriendRequestService(FriendRequestService friendRequestService) {
        this.friendRequestService = friendRequestService;
    }
    public String requestList(){
        HttpServletRequest request= ServletActionContext.getRequest();
        User user= (User) request.getSession().getAttribute("loginUser");
        request.setAttribute("myRequest",friendRequestService.getMyRequest(user.getUid()));
        request.setAttribute("request",friendRequestService.getReceiveRequest(user.getUid()));
        return "success";
    }

    public String accept(){
        friendRequestService.dealRequest(id,true,receiverGid);
        return "accept";
    }
    public String refuse(){
        friendRequestService.dealRequest(id,false,0);
        return "refuse";
    }
    public String cancel(){
        friendRequestService.finishRequest(id);
        return "cancel";
    }
    public String requestCount(){
        User user= (User) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
        Map<String,String> map=new HashMap<>();
        map.put("count",friendRequestService.getNotFinishCount(user.getUid())+"");
        Gson gson=new Gson();
        result=gson.toJson(map);
        return "success";
    }
}
